﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xcj.Mes.WholeLine.Plc.Events;
using Xcj.Mes.WholeLine.ZH.HG.Shell.Models;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.ViewModels.QuickWearPart
{
    public class QuickWearPartBaseViewModel : BaseViewModel
    {
        private string _deviceType = string.Empty;
        public string DeviceType
        {
            get => _deviceType;
            set
            {
                _deviceType = value;
                RaisePropertyChanged();
            }
        }

        private SubscriptionToken _subscriptionToken = null;

        public ObservableCollection<WearPartData> ProgressDatas { get; set; }

        private string _headerTitle = "易损件名称";
        private string _headerChangeCount = "更换次数";


        public string Header_Title
        {
            get { return _headerTitle; }
            set
            {
                _headerTitle = value;
                RaisePropertyChanged();
            }
        }

        public string Header_ChangeCount
        {
            get { return _headerChangeCount; }
            set
            {
                _headerChangeCount = value;
                RaisePropertyChanged();
            }
        }

        public QuickWearPartBaseViewModel(string deviceType)
        {
            DeviceType = deviceType;
            ProgressDatas = new ObservableCollection<WearPartData>();
            var _settings = ConfigurationManager.AppSettings[$"WearPart_{DeviceType}"];
            
            if (!string.IsNullOrEmpty(_settings))
            {
                var wearPartDatas = _settings.Split('|');
                foreach (var item in wearPartDatas)
                {
                    var nameAndLimit = item.Split(':');
                    string _unit = "次";
                    if (nameAndLimit.Length > 2)
                    {
                        _unit = nameAndLimit[2];
                    }
                    // 易损件数据初始化，ChangeCount暂时用随机数代替，后续关联数据库查询
                    ProgressDatas.Add(new WearPartData { 
                        Title = nameAndLimit[0], 
                        LimitTimes = int.Parse(nameAndLimit[1]),
                        Unit = _unit, 
                        ChangeCount = new Random().Next(0, 100)
                     });
                }
            }

            if (deviceType.Equals("老化"))
            {
                Header_Title = "保养名称";
                Header_ChangeCount = "保养次数";
            }

            //关联数据库查询，当前设备的易损件数据更换次数


            //订阅易损件数据更新事件
            _subscriptionToken = _eventAggregator?.GetEvent<QuickWearPartDataEvent>()?.Subscribe(OnQuickWearPartData);
        }

        private void OnQuickWearPartData((string deviceType, Dictionary<int, int> IndexAndValue) updateWearPartData)
        {
            if (updateWearPartData.deviceType.Equals(DeviceType) && updateWearPartData.IndexAndValue != null)
            { 
                foreach (var item in updateWearPartData.IndexAndValue)
                {
                    if (item.Key < ProgressDatas.Count)
                    {
                        ProgressDatas[item.Key].UsedTimes = item.Value;
                    }
                }
            }
        }
    }
}
